const del = require('del')

const  loadPlugins = require('gulp-load-plugins')
const plugins = loadPlugins()

const browserSync = require('browser-sync')
// 自动创建一个开发服务器
const bs = browserSync.create()

const { src, dest, parallel, series, watch} = require('gulp')

// 页面模板内的数据
const data = {
    menus: [
      {
        name: 'Home',
        icon: 'aperture',
        link: 'index.html'
      },
      {
        name: 'Features',
        link: 'features.html'
      },
      {
        name: 'About',
        link: 'about.html'
      },
      {
        name: 'Contact',
        link: '#',
        children: [
          {
            name: 'Twitter',
            link: 'https://twitter.com/w_zce'
          },
          {
            name: 'About',
            link: 'https://weibo.com/zceme'
          },
          {
            name: 'divider'
          },
          {
            name: 'About',
            link: 'https://github.com/zce'
          }
        ]
      }
    ],
    pkg: require('./package.json'),
    date: new Date()
  }

// 编译文件清理
const clean = () => {
    return del(['dist', 'temp'])
}

// 样式编译 gulp-sass
const style = () => {
    return src('src/assets/styles/*.scss', { base: 'src' })
        .pipe(plugins.sass({ outputStyle: 'expanded' }))
        .pipe(dest('temp'))
        .pipe(bs.reload({stream: true}))
}

// 脚本编译 gulp-babel
const script = () => {
    return src('src/assets/scripts/*.js', { base: 'src' })
        .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
        .pipe(dest('temp'))
        .pipe(bs.reload({stream: true}))
}

// 页面编译 gulp-swig
const page = () => {
    return src('src/**/*.html', { base: 'src'})
        .pipe(plugins.swig({ data, defaults: { cache: false}}))
        .pipe(dest('temp'))
        .pipe(bs.reload({stream: true}))
}

// 图片编译 gulp-imagemin
const image = () => {
    return src('src/assets/images/**', { base: 'src' })
        .pipe(plugins.imagemin())
        .pipe(dest('dist'))
} 

// 文字编译 gulp-imagemin
const font = () => {
    return src('src/assets/fonts/**', { base:'src'})
        .pipe(plugins.imagemin())
        .pipe(dest('dist'))
}

// 其他文件拷贝
const extra = () => {
    return src('public/**', { base: 'public' })
        .pipe(dest('dist'))
}

// 文件引用处理以及文件压缩  gulp-useref
const useref = () => {
    return src('temp/**/*.html', {base: 'temp'})
        // 把构建注释做转换
        .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
        // 压缩文件 文件判断 gulp-if
        // 压缩js gulp-uglify
        .pipe(plugins.if(/\.js$/, plugins.uglify()))
        // 压缩css gulp-clean-css
        .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
        // 压缩html  gulp-htmlmin
        .pipe(plugins.if(/\.html$/, plugins.htmlmin({
            collapseWhitespace: true,
            minifyCSS: true,
            minifyJS: true
        })))
        .pipe(dest('dist'))
}

// 热部署服务器 browser-sync
const serve = () => {
    // 监听文件变化并执行任务
    watch('src/assets/styles/*.scss', style)
    watch('src/assets/scripts/*.js', script)
    watch('src/**/*.html', page)
    watch([
        'src/assets/images/**',
        'src/assets/fonts/**',
        'public/**'
    ], bs.reload)

    bs.init({
        notify: false,
        port: 3080,
        server: {
            baseDir: ['temp', 'src', 'public'],
            routes: {
                '/node_modules': 'node_modules'
            }
        }
    })
}

const compile = parallel( style, script, page)

// 构建任务整合  项目上线前执行
const build = series(
    // 首先清理文件
    clean,
    parallel(
        // 编译文件后压缩
        series(compile, useref),
        image,
        font,
        extra
    )
)

// 开发任务 用于开发测试
const develop = series(compile, serve)

module.exports = {
    clean,
    build,
    develop
}